package com.slink.fxfk.demo.domain.po;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinColumns;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
import javax.persistence.Version;

@Entity
@Table(name = "tb_student")
public class PStudent implements Serializable
{
    private static final long serialVersionUID = 7398795855215458937L;

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Integer id;
    
    @Column(name="s_name", nullable=false, unique=true) //不可为null,不可重复
    private String username;
    
    @Temporal(TemporalType.DATE)
    @Column(name="s_birthday")
    private Date birthday;
    
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name="s_createtime")
    private Date createDate;
    
    @Temporal(TemporalType.TIME)
    @Column(name="s_oprtime")
    private Date operateDate;
    
    // @Basic 可为普通属性配置加载方式，默认即时加载，对于大数据列如LOB此为延迟加载且可为null
    @Basic(fetch=FetchType.LAZY, optional=true) 
    @Column(name="s_courses")
    private List<PCourse> courses;
    
    @Transient 
    private String temAttribute; //临时属性，不会写入到数据库
    
    //乐观锁
    @Version
    private Integer version;
    
    //单边一对多
    @OneToMany(fetch = FetchType.EAGER,//一对多配置并配置列关联
    			targetEntity=PEmail.class,
    			cascade={ //等同于 CascadeType.ALL
    	CascadeType.PERSIST,CascadeType.REMOVE,CascadeType.MERGE,
    	CascadeType.REFRESH
    })
    @JoinColumns(value={@JoinColumn(name="student_id", referencedColumnName="id")})
    @OrderBy(value="email desc") // 按照email列降序排列
    private List<PEmail> emails = new ArrayList<PEmail>();

    //单边多对一
    @ManyToOne(cascade={CascadeType.PERSIST}, //如果Teacher对象没有被持久化，则持久化student对象将一并持久化Teacher对象
    			fetch=FetchType.EAGER
    		)
    @JoinColumn(name="teacher_id")//外键为 teacher_id
    private PTeacher teacher;
    
    @Column(columnDefinition="text")//描述内容可能较长
    private String description;
    
    ////双边多对一，一对多关系
    //学生与班级是多对一关系
    @ManyToOne(fetch=FetchType.EAGER,cascade={CascadeType.PERSIST}) //配置级联保存类型为PERSIST，创建student时会自动将clazz也写进数据库
    @JoinColumn(name="class_id") //外键为class_id，与tb_clazz主键列连接
    private PCalzz calzz;
    
    public List<PEmail> getEmails() {
		return emails;
	}

	public void setEmails(List<PEmail> emails) {
		this.emails = emails;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public Date getCreateDate() {
		return createDate;
	}

	public void setCreateDate(Date createDate) {
		this.createDate = createDate;
	}

	public Date getOperateDate() {
		return operateDate;
	}

	public void setOperateDate(Date operateDate) {
		this.operateDate = operateDate;
	}

	public List<PCourse> getCourses() {
		return courses;
	}

	public void setCourses(List<PCourse> courses) {
		this.courses = courses;
	}

	public String getTemAttribute() {
		return temAttribute;
	}

	public void setTemAttribute(String temAttribute) {
		this.temAttribute = temAttribute;
	}

	public Integer getVersion() {
		return version;
	}

	public void setVersion(Integer version) {
		this.version = version;
	}

	public Integer getId()
    {
        return id;
    }

    public void setId(Integer id)
    {
        this.id = id;
    }

    public String getUsername()
    {
        return username;
    }

    public void setUsername(String username)
    {
        this.username = username;
    }
 
}
